package org.dizena.modules.master.service;

import org.apache.commons.lang3.StringUtils;
import org.dizena.base.avo.req.JqGridReq;
import org.dizena.base.avo.resp.JqGridResp;
import org.dizena.modules.master.bean.Res;
import org.dizena.modules.master.dao.RessDao;
import org.dizena.utils.IdGen;
import org.dizena.utils.JqGridRespBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RessService
{

    @Resource
    private RessDao ressDao;
    @Resource
    private JqGridRespBuilder<Res> jqGridRespBuilder;

    public JqGridResp<Res> list(JqGridReq req, String[] keys, Object[] values)
    {
        return jqGridRespBuilder.build(req, ressDao, keys, values);
    }

    public String crud(String oper, Res ress)
    {
        if ("add".equals(oper))
        {
            ress.setId(IdGen.get().nextIdS());
            if (StringUtils.isEmpty(ress.getResPid()))
            {
                ress.setResPid("0");
                ress.setResLevel(1);
            }
            else
            {
                ress.setResLevel(2);
            }
            if (StringUtils.isNotEmpty(ress.getResIcon()) && ress.getResIcon().contains("\""))
            {
                String s1 = ress.getResIcon();
                int i1 = s1.indexOf("\"");
                int i2 = s1.lastIndexOf("\"");
                String icon = s1.substring(i1 + 1, i2);
                ress.setResIcon(icon);
            }
            ressDao.add(ress);
        }
        else if ("edit".equals(oper))
        {
            if (StringUtils.isNotEmpty(ress.getResIcon()) && ress.getResIcon().contains("\""))
            {
                String s1 = ress.getResIcon();
                int i1 = s1.indexOf("\"");
                int i2 = s1.lastIndexOf("\"");
                String icon = s1.substring(i1 + 1, i2);
                ress.setResIcon(icon);
            }
            ressDao.update(ress);
        }
        else if ("del".equals(oper))
        {
            String ids = ress.getId();
            for (String s : ids.split(","))
            {
                ressDao.deleteById(s);
                List<Res> subs = queryResSubs(s);
                subs.forEach(r ->
                {
                    ressDao.deleteById(r.getId());
                });
            }
        }
        return "ok";
    }

    public List<Res> queryResSubs(String resPid)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("resPid", resPid);
        //map.put("resLevel", resLevel);
        JqGridReq req = new JqGridReq();
        req.setSord("ASC");
        req.setSidx("resSort");
        return ressDao.queryAll(req, map);
    }

    public Integer queryMaxSort(String pid)
    {
        Integer res = ressDao.queryMaxSort(pid);
        if (res == null)
        {
            return ressDao.get(pid).getResSort() * 100 + 1;
        }
        return res + 1;
    }

    public List<Res> queryParentRes(Integer level)
    {
        List<Res> ress = new ArrayList<Res>();
        if (1 == level)
        {
            Res r = new Res("0", "根目录", null, null, null, null, null);
            ress.add(r);
        }
        else
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("resLevel", level - 1);
            JqGridReq req = new JqGridReq();
            req.setSord("ASC");
            req.setSidx("resSort");
            ress = ressDao.queryAll(req, map);
        }
        return ress;
    }

}
